var data={};
var fi=1;
var pagenum= 10;
var company_name='';
var service_mode="{$Think.get.service_mode}";
var link = "{$Think.get.link}";
var url={
    list:api.facilitator.list,
    delete:api.facilitator.delete
}
//搜索条件
var where_json ={};
if(service_mode){
    where_json.service_mode=['in',service_mode]
}

if(link){
    where_json.link_man=['like','%'+link+'%']
    where_json.link_phone=['like','%'+link+'%']
}
data.where_json=JSON.stringify(where_json)
   
// 主页
    ajax(api.config.data,{
        page:1,
        pagenum:99999
    },'post').done(function(res){
       var  options =`<option value="0" selected>全部-服务商</option>`;
       for (const key in res.data.service_mode) {
         options+=`<option  value="${key}">${res.data.service_mode[key]}</option>`
        }
        $('.service_mode').html(options)
        form.render('select');
    }).fail(function(err){
        Error('请求失败')
    });

    var main=[[ //表头
        {type:'checkbox', fixed: 'left'}
       ,{field: 'service_name', title: '服务商', fixed: 'left'}
       ,{field: 'link_man', title: '联系人',width:100}
       ,{field: 'link_phone', title: '联系电话',}
       ,{field: 'create_time', title: '创建时间',width:170}
       ,{field: 'update_time', title: '更新时间',width:170}
       ,{field: 'address', title: '地址',}
       ,{title: '操作', toolbar:'#operation',fixed: 'right',align:'center'}
    ]];

    ajaxpacilitator(data)
    function ajaxpacilitator(data){
        Load();
        ajax(url.list, data, 'post').done(function (res) {
            Load(1);
            console.log(res)
            if(res.code ==1){
                 // 添加服务商跳转
                 $('#add').on('click',()=>{
                    window.location.href = path.facilitator.add+'.html';
                })
                table.render({
                    elem: '#service',
                    cols: main,
                    data: res.data,
                    limit: pagenum,
                    cellMinWidth: 80
                });
                if (fi == 1) {
                    laypage.render({
                        elem: 'page',
                        count: res.count,
                        layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                        theme: '#1E9FFF',
                        jump: function (obj, first) {
                            console.log(obj)
                            if (!first) {
                                page = obj.curr;
                                data.page = page;
                                pagenum = obj.limit;
                                data.pagenum = pagenum;
                                ajaxpacilitator(data)
                            } else {
                
                            }
                        }
                    })
                    fi = 2;
                }
                   // 操作
        table.on('tool(operation)', function (obj) {
            var data1 = obj.data; //获得当前行数据
            var data2 = {
                service_id:data1.service_id
            };
            var data3={
                ids:data1.service_id
            }
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            console.log(data2)
            console.log(data1)
            if (layEvent === 'edit') {
                window.location.href = path.facilitator.add + "?service_id=" + data1.service_id;
            } else if (layEvent === 'del') {
                console.log('删除')
                layer.confirm('是否删除', function (index) {
                    Load()
                    ajax(url.delete, data3, 'post').done(function (res) {
                        // 成功回调
                        console.log(res)
                        layer.closeAll();
                        Msg('删除成功');
                        ajaxpacilitator(data)
                    }).fail(function (err) {
                        // 失败回调
                        layer.close(deleteload);
                        Err(res.msg ? res.msg : '操作失败，稍候再试');
                    });
                    layer.close(index);
                })
            }
        })
          		//删除多个
    			$(".delete-multiple").on('click', function () {
    				var ids = '';
                    var checkStatus = table.checkStatus('service'); //test即为基础参数id对应的值
                    console.log('多选信息')
                    console.log(checkStatus)
    				console.log(checkStatus.data) //获取选中行的数据
    				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                    console.log(checkStatus.isAll) //表格是否全选
                    if(checkStatus.data.length){
    				for (var dei = 0; dei < checkStatus.data.length; dei++) {
    					if (dei == 0) {
    						ids = checkStatus.data[dei].service_id
    					} else {
    						ids += ("," + checkStatus.data[dei].service_id)
    					}
                    }
                    console.log('删除')
                    
                    var data2 ={
                        ids : ids
                    }
    				layer.open({
    					type: 1,
    					title: false,
    					id: "delete",
    					content: '<div style="padding: 20px 100px;">是否删除</div>',
    					btn: ['确定', '取消'],
    					btnAlign: 'c' //按钮居中
    						,
    					shade: 0 //不显示遮罩
    						,
    					yes: function () {
    						var deleteload = layer.load(1, {
    							shade: [0.1, '#fff'] //0.1透明度的白色背景
                            });
                            console.log(data2)
                            ajax(url.delete,data2,'post').done(function(res) {
                                // 成功回调
                            			layer.close(deleteload);
                                        layer.closeAll();
                                        Msg('删除成功');
                                        ajaxpacilitator(data)
                                }).fail(function(err) {
                                // 失败回调
                                layer.close(deleteload);
    						    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
    					},
    					btn2: function () {
    						layer.closeAll();
    					}
                    });
                }else{
                    Err('未有选择项')
                }
    			});

            }else{
                Err('请求失败')
            }
               

        }).fail(function(err){

        })
    }
    


     
   
   
     
      


